<template>
	<div class="cms-channel-container">
		<el-card shadow="hover" class="mb10" v-auth="'cms:channel:list'">
			<el-form :inline="true" :model="search">
				<el-form-item>
					<el-input v-model="search.name" placeholder="请输入栏目名称" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
					<el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="hover">
			<el-button v-auth="'cms:channel:add'" type="primary" icon="el-icon-document-add" @click="onOpenAddChannel()">新增</el-button>
			<el-table ref="tableChannelRef" stripe style="width: 100%" row-key="id" 
				:data="tableData.data"
				:expand-row-keys="[tableData.data[0]?.id]" 
				:tree-props="{ children: 'children'}"
				@row-click="onTableRowClick"
				>
				<el-table-column label="栏目名称" prop="name" show-overflow-tooltip min-width="250"></el-table-column>
				<el-table-column label="栏目编码" prop="id" show-overflow-tooltip width="250">
					<template #default="scope">
						<el-button type="primary" link @click="onOpenCopyChannel(scope.row)">{{scope.row.id}}</el-button>
					</template>
				</el-table-column>
				<el-table-column label="类型" prop="type" show-overflow-tooltip width="100">
					<template #default="scope">
						<span v-if="scope.row.type == '1'" class="color-primary">站点栏目</span>
						<span v-else-if="scope.row.type == '2'">普通栏目</span>
						<span v-else-if="scope.row.type == '3'" class="color-success">首页栏目</span>
						<span v-else-if="scope.row.type == '4'" class="color-info">隐藏栏目</span>
						<span v-else-if="scope.row.type == '5'" class="color-warning">专题栏目</span>
						<span v-else>未知类型</span>
					</template>
				</el-table-column>
				<el-table-column label="状态" show-overflow-tooltip width="80">
					<template #default="scope">
						<span v-if="scope.row.status == '0'" class="color-primary">正常</span>
						<span v-else-if="scope.row.status == '1'" class="color-info">停用</span>
						<span v-else class="color-danger">删除</span>
					</template>
				</el-table-column>
				<el-table-column label="排序" prop="orderNum" width="80" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="180"
					v-auths="['cms:channel:add', 'cms:site:edit', 'cms:channel:edit', 'cms:channel:remove']">
					<template #default="scope">
						<el-button v-auth="'cms:channel:add'" type="primary" link @click="onOpenAddChannel(scope.row)">新增</el-button>
						<el-button v-if="scope.row.parentId == 0" v-auth="'cms:site:edit'" type="primary" link @click="onOpenEditSite(scope.row)">修改</el-button>
						<el-button v-if="scope.row.parentId != 0" v-auth="'cms:channel:edit'" type="primary" link @click="onOpenEditChannel(scope.row)">修改</el-button>
						<el-button v-if="scope.row.parentId != 0" v-auth="'cms:channel:remove'" type="primary" link @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<AddChannel ref="addChannelRef" @submit="onTableSearch"/>
		<EditChannel ref="editChannelRef" @submit="onTableSearch"/>
		<EditSite ref="editSiteRef" @submit="onTableSearch"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted, defineComponent } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddChannel from '/@/views/cms/channel/addChannel.vue';
import EditChannel from '/@/views/cms/channel/editChannel.vue';
import EditSite from '/@/views/cms/channel/editSite.vue';
import { getChannelList, delChannel } from '/@/api/cms/channel/index';
import { lineToTreeData, copyText } from '/@/utils/commonFunction';
export default defineComponent({
	name: 'cms/channel',
	props: {
		// 绑定值
		model: {
			type: Object,
			default: () => {},
		},
	},
	components: { AddChannel, EditChannel, EditSite },
	setup(props, context) {
		const tableChannelRef = ref();
		const addChannelRef = ref();
		const editChannelRef = ref();
		const editSiteRef = ref();
		const state = reactive({
			search:{
				siteId: '',
				name: ''
			},
			tableData: {
				data: [] as any,
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params?: object) => {
			getChannelList(Object.assign(state.search, params)).then((res) => {
				if(res.code == 0){
					state.tableData.data = lineToTreeData(res.data);
					state.tableData.total = res.total;
				}
			});
		}

		//查询
		const onTableSearch = () => {
			loadTableData(state.search);
			context.emit('submit');
		}
		//重置
		const onResetSearch = () => {
			state.search.name = '';
			loadTableData();
		}
		// 复制编码
		const onOpenCopyChannel = (row: object) => {
			copyText(row.id);
		}
		// 打开新增栏目弹窗
		const onOpenAddChannel = (row?: object) => {
			addChannelRef.value.openDialog(row || props.model);
		};
		// 打开编辑栏目弹窗
		const onOpenEditChannel = (row: object) => {
			editChannelRef.value.openDialog(row);
		};
		const onOpenEditSite = (row: object) => {
			editSiteRef.value.openDialog(row);
		}
		// 删除当前行
		const onTabelRowDel = (row: object) => {
			ElMessageBox.confirm('确定删除该条【' + row.name + '】栏目信息吗？', '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delChannel([row.id]).then((res) => {
					if(res.code === 0){
						ElMessage.success('删除成功');
						onTableSearch();
					}
				});
			}).catch(() => {});
		};
		// 当行点击时
		const onTableRowClick = (row: object) => {
			
		}
		// 页面加载时
		onMounted(() => {
			state.search.siteId = props.model.siteId;
			loadTableData();
		});
		
		return {
			tableChannelRef,
			addChannelRef,
			editChannelRef,
			editSiteRef,
			onTableSearch,
			onResetSearch,
			onOpenCopyChannel,
			onOpenAddChannel,
			onOpenEditChannel,
			onOpenEditSite,
			onTabelRowDel,
			onTableRowClick,
			...toRefs(state),
		};
	},
});
</script>